
#region using Statements

using System;
using System.Xml;
using System.IO;

#endregion

public class XMLSearch
{
    #region Declarations & Constructor

    private XmlDocument _doc = new XmlDocument();

    public XMLSearch(string DocumentPath)
    {
        DocumentPath = String.Format("{0}App_Data\\{1}", AppDomain.CurrentDomain.BaseDirectory, DocumentPath);

        if (!File.Exists(DocumentPath))
        {
            throw new FileNotFoundException(String.Format("Cannot find specified file {0}", DocumentPath));
        }
        else
        {
            this._doc.Load(DocumentPath);
        }
    }
    #endregion

    public string GetWindowsLiveID(string LocalID)
    {
        // Creating xPathQuery to retrieve node with record containing appropriate student ID
        // Also making xPath query case in-sensitive
        string xPathQuery =
            String.Format("/students/entry[translate(@sAMAccountName, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = '{0}']", LocalID.ToLower());

        string result = String.Empty;
        try
        {
            // Retrieving single node 
            XmlNode record = this._doc.SelectSingleNode(xPathQuery);
            result = record.Attributes["windowsLiveID"].Value;
        }
        catch (NullReferenceException) { }

        return result;
    }
}